home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / bash / bash-110 / mintve~1 / readline.zoo / readline / doc / readline.info < prev    next >
Encoding:
Text File  |  1991-07-30  |  61.3 KB  |  1,721 lines

  1. Info file readline.info, produced by Makeinfo, -*- Text -*- from input
  2. file rlman.texinfo.
  3.  
  4.    This document describes the GNU Readline Library, a utility which
  5. aids in the consistency of user interface across discrete programs
  6. that need to provide a command line interface.
  7.  
  8.    Copyright (C) 1988, 1991 Free Software Foundation, Inc.
  9.  
  10.    Permission is granted to make and distribute verbatim copies of
  11. this manual provided the copyright notice and this permission notice
  12. pare preserved on all copies.
  13.  
  14.    Permission is granted to copy and distribute modified versions of
  15. this manual under the conditions for verbatim copying, provided that
  16. the entire resulting derived work is distributed under the terms of a
  17. permission notice identical to this one.
  18.  
  19.    Permission is granted to copy and distribute translations of this
  20. manual into another language, under the above conditions for modified
  21. versions, except that this permission notice may be stated in a
  22. translation approved by the Foundation.
  23.  
  24. File: readline.info,  Node: Top,  Next: Command Line Editing,  Prev: (DIR),  Up: (DIR)
  25.  
  26. GNU Readline Library
  27. ********************
  28.  
  29.    This document describes the GNU Readline Library, a utility which
  30. aids in the consistency of user interface across discrete programs
  31. that need to provide a command line interface.
  32.  
  33. * Menu:
  34.  
  35. * Command Line Editing::       GNU Readline User's Manual.
  36. * Programming with GNU Readline::  GNU Readline Programmer's Manual.
  37. * Concept Index::           Index of concepts described in this manual.
  38. * Function and Variable Index::       Index of externally visible functions
  39.                    and variables.
  40.  
  41. File: readline.info,  Node: Command Line Editing,  Next: Programming with GNU Readline,  Prev: Top,  Up: Top
  42.  
  43. Command Line Editing
  44. ********************
  45.  
  46.    This text describes GNU's command line editing interface.
  47.  
  48. * Menu:
  49.  
  50. * Introduction and Notation::    Notation used in this text.
  51. * Readline Interaction::    The minimum set of commands for editing a line.
  52. * Readline Init File::        Customizing Readline from a user's view.
  53.  
  54. File: readline.info,  Node: Introduction and Notation,  Next: Readline Interaction,  Up: Command Line Editing
  55.  
  56. Introduction to Line Editing
  57. ============================
  58.  
  59.    The following paragraphs describe the notation we use to represent
  60. keystrokes.
  61.  
  62.    The text C-k is read as `Control-K' and describes the character
  63. produced when the Control key is depressed and the k key is struck.
  64.  
  65.    The text M-k is read as `Meta-K' and describes the character
  66. produced when the meta key (if you have one) is depressed, and the k
  67. key is struck.  If you do not have a meta key, the identical keystroke
  68. can be generated by typing ESC first, and then typing k.  Either
  69. process is known as "metafying" the k key.
  70.  
  71.    The text M-C-k is read as `Meta-Control-k' and describes the
  72. character produced by "metafying" C-k.
  73.  
  74.    In addition, several keys have their own names.  Specifically, DEL,
  75. ESC, LFD, SPC, RET, and TAB all stand for themselves when seen in this
  76. text, or in an init file (*note Readline Init File::., for more info).
  77.  
  78. File: readline.info,  Node: Readline Interaction,  Next: Readline Init File,  Prev: Introduction and Notation,  Up: Command Line Editing
  79.  
  80. Readline Interaction
  81. ====================
  82.  
  83.    Often during an interactive session you type in a long line of text,
  84. only to notice that the first word on the line is misspelled.  The
  85. Readline library gives you a set of commands for manipulating the text
  86. as you type it in, allowing you to just fix your typo, and not forcing
  87. you to retype the majority of the line.  Using these editing commands,
  88. you move the cursor to the place that needs correction, and delete or
  89. insert the text of the corrections.  Then, when you are satisfied with
  90. the line, you simply press RETURN.  You do not have to be at the end
  91. of the line to press RETURN; the entire line is accepted regardless of
  92. the location of the cursor within the line.
  93.  
  94. * Menu:
  95.  
  96. * Readline Bare Essentials::    The least you need to know about Readline.
  97. * Readline Movement Commands::    Moving about the input line.
  98. * Readline Killing Commands::    How to delete text, and how to get it back!
  99. * Readline Arguments::        Giving numeric arguments to commands.
  100.  
  101. File: readline.info,  Node: Readline Bare Essentials,  Next: Readline Movement Commands,  Up: Readline Interaction
  102.  
  103. Readline Bare Essentials
  104. ------------------------
  105.  
  106.    In order to enter characters into the line, simply type them.  The
  107. typed character appears where the cursor was, and then the cursor
  108. moves one space to the right.  If you mistype a character, you can use
  109. DEL to back up, and delete the mistyped character.
  110.  
  111.    Sometimes you may miss typing a character that you wanted to type,
  112. and not notice your error until you have typed several other
  113. characters.  In that case, you can type C-b to move the cursor to the
  114. left, and then correct your mistake.  Aftwerwards, you can move the
  115. cursor to the right with C-f.
  116.  
  117.    When you add text in the middle of a line, you will notice that
  118. characters to the right of the cursor get `pushed over' to make room
  119. for the text that you have inserted.  Likewise, when you delete text
  120. behind the cursor, characters to the right of the cursor get `pulled
  121. back' to fill in the blank space created by the removal of the text. 
  122. A list of the basic bare essentials for editing the text of an input
  123. line follows.
  124.  
  125. C-b
  126.      Move back one character.
  127.  
  128. C-f
  129.      Move forward one character.
  130.  
  131. DEL
  132.      Delete the character to the left of the cursor.
  133.  
  134. C-d
  135.      Delete the character underneath the cursor.
  136.  
  137. Printing characters
  138.      Insert itself into the line at the cursor.
  139.  
  140. C-_
  141.      Undo the last thing that you did.  You can undo all the way back
  142.      to an empty line.
  143.  
  144. File: readline.info,  Node: Readline Movement Commands,  Next: Readline Killing Commands,  Prev: Readline Bare Essentials,  Up: Readline Interaction
  145.  
  146. Readline Movement Commands
  147. --------------------------
  148.  
  149.    The above table describes the most basic possible keystrokes that
  150. you need in order to do editing of the input line.  For your
  151. convenience, many other commands have been added in addition to C-b,
  152. C-f, C-d, and DEL.  Here are some commands for moving more rapidly
  153. about the line.
  154.  
  155. C-a
  156.      Move to the start of the line.
  157.  
  158. C-e
  159.      Move to the end of the line.
  160.  
  161. M-f
  162.      Move forward a word.
  163.  
  164. M-b
  165.      Move backward a word.
  166.  
  167. C-l
  168.      Clear the screen, reprinting the current line at the top.
  169.  
  170.    Notice how C-f moves forward a character, while M-f moves forward a
  171. word.  It is a loose convention that control keystrokes operate on
  172. characters while meta keystrokes operate on words.
  173.  
  174. File: readline.info,  Node: Readline Killing Commands,  Next: Readline Arguments,  Prev: Readline Movement Commands,  Up: Readline Interaction
  175.  
  176. Readline Killing Commands
  177. -------------------------
  178.  
  179.    The act of "cutting" text means to delete the text from the line,
  180. and to save away the deleted text for later use, just as if you had
  181. cut the text out of the line with a pair of scissors.  There is a
  182.  
  183.    "Killing" text means to delete the text from the line, but to save
  184. it away for later use, usually by "yanking" it back into the line.  If
  185. the description for a command says that it `kills' text, then you can
  186. be sure that you can get the text back in a different (or the same)
  187. place later.
  188.  
  189.    Here is the list of commands for killing text.
  190.  
  191. C-k
  192.      Kill the text from the current cursor position to the end of the
  193.      line.
  194.  
  195. M-d
  196.      Kill from the cursor to the end of the current word, or if between
  197.      words, to the end of the next word.
  198.  
  199. M-DEL
  200.      Kill fromthe cursor the start of the previous word, or if between
  201.      words, to the start of the previous word.
  202.  
  203. C-w
  204.      Kill from the cursor to the previous whitespace.  This is
  205.      different than M-DEL because the word boundaries differ.
  206.  
  207.    And, here is how to "yank" the text back into the line.  Yanking is
  208.  
  209. C-y
  210.      Yank the most recently killed text back into the buffer at the
  211.      cursor.
  212.  
  213. M-y
  214.      Rotate the kill-ring, and yank the new top.  You can only do this
  215.      if the prior command is C-y or M-y.
  216.  
  217.    When you use a kill command, the text is saved in a "kill-ring". 
  218. Any number of consecutive kills save all of the killed text together,
  219. so that when you yank it back, you get it in one clean sweep.  The kill
  220. ring is not line specific; the text that you killed on a previously
  221. typed line is available to be yanked back later, when you are typing
  222. another line.
  223.  
  224. File: readline.info,  Node: Readline Arguments,  Prev: Readline Killing Commands,  Up: Readline Interaction
  225.  
  226. Readline Arguments
  227. ------------------
  228.  
  229.    You can pass numeric arguments to Readline commands.  Sometimes the
  230. argument acts as a repeat count, other times it is the sign of the
  231. argument that is significant.  If you pass a negative argument to a
  232. command which normally acts in a forward direction, that command will
  233. act in a backward direction.  For example, to kill text back to the
  234. start of the line, you might type M-- C-k.
  235.  
  236.    The general way to pass numeric arguments to a command is to type
  237. meta digits before the command.  If the first `digit' you type is a
  238. minus sign (-), then the sign of the argument will be negative.  Once
  239. you have typed one meta digit to get the argument started, you can type
  240. the remainder of the digits, and then the command.  For example, to
  241. give the C-d command an argument of 10, you could type M-1 0 C-d.
  242.  
  243. File: readline.info,  Node: Readline Init File,  Prev: Readline Interaction,  Up: Command Line Editing
  244.  
  245. Readline Init File
  246. ==================
  247.  
  248.    Although the Readline library comes with a set of Emacs-like
  249. keybindings, it is possible that you would like to use a different set
  250. of keybindings.  You can customize programs that use Readline by
  251. putting commands in an "init" file in your home directory.  The name
  252. of this file is `~/.inputrc'.
  253.  
  254.    When a program which uses the Readline library starts up, the
  255. `~/.inputrc' file is read, and the keybindings are set.
  256.  
  257.    In addition, the `C-x C-r' command re-reads this init file, thus
  258. incorporating any changes that you might have made to it.
  259.  
  260. * Menu:
  261.  
  262. * Readline Init Syntax::    Syntax for the commands in `~/.inputrc'.
  263. * Readline Vi Mode::        Switching to `vi' mode in Readline.
  264.  
  265. File: readline.info,  Node: Readline Init Syntax,  Next: Readline Vi Mode,  Up: Readline Init File
  266.  
  267. Readline Init Syntax
  268. --------------------
  269.  
  270.    There are only four constructs allowed in the `~/.inputrc' file:
  271.  
  272. Variable Settings
  273.      You can change the state of a few variables in Readline.  You do
  274.      this by using the `set' command within the init file.  Here is
  275.      how you would specify that you wish to use Vi line editing
  276.      commands:
  277.  
  278.           set editing-mode vi
  279.  
  280.           Right now, there are only a few variables which can be set; so
  281.      few in fact, that we just iterate them here:
  282.  
  283.     `editing-mode'
  284.           The `editing-mode' variable controls which editing mode you
  285.           are using.  By default, GNU Readline starts up in Emacs
  286.           editing mode, where the keystrokes are most similar to
  287.           Emacs.  This variable can either be set to `emacs' or `vi'.
  288.  
  289.     `horizontal-scroll-mode'
  290.           This variable can either be set to `On' or `Off'.  Setting it
  291.           to `On' means that the text of the lines that you edit will
  292.           scroll horizontally on a single screen line when they are
  293.           larger than the width of the screen, instead of wrapping
  294.           onto a new screen line.  By default, this variable is set to
  295.           `Off'.
  296.  
  297.     `mark-modified-lines'
  298.           This variable when set to `On', says to display an asterisk
  299.           (`*') at the starts of history lines which have been
  300.           modified.  This variable is off by default.
  301.  
  302.     `prefer-visible-bell'
  303.           If this variable is set to `On' it means to use a visible
  304.           bell if one is available, rather than simply ringing the
  305.           terminal bell.  By default, the value is `Off'.
  306.  
  307. Key Bindings
  308.      The syntax for controlling keybindings in the `~/.inputrc' file is
  309.      simple.  First you have to know the name of the command that you
  310.      want to change.  The following pages contain tables of the
  311.      command name, the default keybinding, and a short description of
  312.      what the command does.
  313.  
  314.      Once you know the name of the command, simply place the name of
  315.      the key you wish to bind the command to, a colon, and then the
  316.      name of the command on a line in the `~/.inputrc' file.  The name
  317.      of the key can be expressed in different ways, depending on which
  318.      is most comfortable for you.
  319.  
  320.     KEYNAME: FUNCTION-NAME or MACRO
  321.           KEYNAME is the name of a key spelled out in English.  For
  322.           example:
  323.  
  324.                Control-u: universal-argument
  325.                Meta-Rubout: backward-kill-word
  326.                Control-o: ">&output"
  327.  
  328.                     In the above example, `C-u' is bound to the function
  329.           `universal-argument', and `C-o' is bound to run the macro
  330.           expressed on the right hand side (that is, to insert the text
  331.           `>&output' into the line).
  332.  
  333.     "KEYSEQ": FUNCTION-NAME or MACRO
  334.           KEYSEQ differs from KEYNAME above in that strings denoting
  335.           an entire key sequence can be specified.  Simply place the
  336.           key sequence in double quotes.  GNU Emacs style key escapes
  337.           can be used, as in the following example:
  338.  
  339.                "\C-u": universal-argument
  340.                "\C-x\C-r": re-read-init-file
  341.                "\e[11~": "Function Key 1"
  342.  
  343.                     In the above example, `C-u' is bound to the function
  344.           `universal-argument' (just as it was in the first example),
  345.           `C-x C-r' is bound to the function `re-read-init-file', and
  346.           `ESC [ 1 1 ~' is bound to insert the text `Function Key 1'.
  347.  
  348. * Menu:
  349.  
  350. * Commands For Moving::        Moving about the line.
  351. * Commands For History::    Getting at previous lines.
  352. * Commands For Text::        Commands for changing text.
  353. * Commands For Killing::    Commands for killing and yanking.
  354. * Numeric Arguments::        Specifying numeric arguments, repeat counts.
  355. * Commands For Completion::    Getting Readline to do the typing for you.
  356. * Miscellaneous Commands::    Other miscillaneous commands.
  357.  
  358. File: readline.info,  Node: Commands For Moving,  Next: Commands For History,  Up: Readline Init Syntax
  359.  
  360. Commands For Moving
  361. ...................
  362.  
  363. `beginning-of-line (C-a)'
  364.      Move to the start of the current line.
  365.  
  366. `end-of-line (C-e)'
  367.      Move to the end of the line.
  368.  
  369. `forward-char (C-f)'
  370.      Move forward a character.
  371.  
  372. `backward-char (C-b)'
  373.      Move back a character.
  374.  
  375. `forward-word (M-f)'
  376.      Move forward to the end of the next word.
  377.  
  378. `backward-word (M-b)'
  379.      Move back to the start of this, or the previous, word.
  380.  
  381. `clear-screen (C-l)'
  382.      Clear the screen leaving the current line at the top of the
  383.      screen.
  384.  
  385. File: readline.info,  Node: Commands For History,  Next: Commands For Text,  Prev: Commands For Moving,  Up: Readline Init Syntax
  386.  
  387. Commands For Manipulating The History
  388. .....................................
  389.  
  390. `accept-line (Newline, Return)'
  391.      Accept the line regardless of where the cursor is.  If this line
  392.      is non-empty, add it to the history list.  If this line was a
  393.      history line, then restore the history line to its original state.
  394.  
  395. `previous-history (C-p)'
  396.      Move `up' through the history list.
  397.  
  398. `next-history (C-n)'
  399.      Move `down' through the history list.
  400.  
  401. `beginning-of-history (M-<)'
  402.      Move to the first line in the history.
  403.  
  404. `end-of-history (M->)'
  405.      Move to the end of the input history, i.e., the line you are
  406.      entering!
  407.  
  408. `reverse-search-history (C-r)'
  409.      Search backward starting at the current line and moving `up'
  410.      through the history as necessary.  This is an incremental search.
  411.  
  412. `forward-search-history (C-s)'
  413.      Search forward starting at the current line and moving `down'
  414.      through the the history as neccessary.
  415.  
  416. File: readline.info,  Node: Commands For Text,  Next: Commands For Killing,  Prev: Commands For History,  Up: Readline Init Syntax
  417.  
  418. Commands For Changing Text
  419. ..........................
  420.  
  421. `delete-char (C-d)'
  422.      Delete the character under the cursor.  If the cursor is at the
  423.      beginning of the line, and there are no characters in the line,
  424.      and the last character typed was not C-d, then return EOF.
  425.  
  426. `backward-delete-char (Rubout)'
  427.      Delete the character behind the cursor.  A numeric arg says to
  428.      kill the characters instead of deleting them.
  429.  
  430. `quoted-insert (C-q, C-v)'
  431.      Add the next character that you type to the line verbatim.  This
  432.      is how to insert things like C-q for example.
  433.  
  434. `tab-insert (M-TAB)'
  435.      Insert a tab character.
  436.  
  437. `self-insert (a, b, A, 1, !, ...)'
  438.      Insert yourself.
  439.  
  440. `transpose-chars (C-t)'
  441.      Drag the character before point forward over the character at
  442.      point.  Point moves forward as well.  If point is at the end of
  443.      the line, then transpose the two characters before point. 
  444.      Negative args don't work.
  445.  
  446. `transpose-words (M-t)'
  447.      Drag the word behind the cursor past the word in front of the
  448.      cursor moving the cursor over that word as well.
  449.  
  450. `upcase-word (M-u)'
  451.      Uppercase the current (or following) word.  With a negative
  452.      argument, do the previous word, but do not move point.
  453.  
  454. `downcase-word (M-l)'
  455.      Lowercase the current (or following) word.  With a negative
  456.      argument, do the previous word, but do not move point.
  457.  
  458. `capitalize-word (M-c)'
  459.      Uppercase the current (or following) word.  With a negative
  460.      argument, do the previous word, but do not move point.
  461.  
  462. File: readline.info,  Node: Commands For Killing,  Next: Numeric Arguments,  Prev: Commands For Text,  Up: Readline Init Syntax
  463.  
  464. Killing And Yanking
  465. ...................
  466.  
  467. `kill-line (C-k)'
  468.      Kill the text from the current cursor position to the end of the
  469.      line.
  470.  
  471. `backward-kill-line ()'
  472.      Kill backward to the beginning of the line.  This is normally
  473.      unbound.
  474.  
  475. `kill-word (M-d)'
  476.      Kill from the cursor to the end of the current word, or if between
  477.      words, to the end of the next word.
  478.  
  479. `backward-kill-word (M-DEL)'
  480.      Kill the word behind the cursor.
  481.  
  482. `unix-line-discard (C-u)'
  483.      Do what C-u used to do in Unix line input.  We save the killed
  484.      text on the kill-ring, though.
  485.  
  486. `unix-word-rubout (C-w)'
  487.      Do what C-w used to do in Unix line input.  The killed text is
  488.      saved on the kill-ring.  This is different than
  489.      backward-kill-word because the word boundaries differ.
  490.  
  491. `yank (C-y)'
  492.      Yank the top of the kill ring into the buffer at point.
  493.  
  494. `yank-pop (M-y)'
  495.      Rotate the kill-ring, and yank the new top.  You can only do this
  496.      if the prior command is yank or yank-pop.
  497.  
  498. File: readline.info,  Node: Numeric Arguments,  Next: Commands For Completion,  Prev: Commands For Killing,  Up: Readline Init Syntax
  499.  
  500. Specifying Numeric Arguments
  501. ............................
  502.  
  503. `digit-argument (M-0, M-1, ... M--)'
  504.      Add this digit to the argument already accumulating, or start a
  505.      new argument.  M-- starts a negative argument.
  506.  
  507. `universal-argument ()'
  508.      Do what C-u does in emacs.  By default, this is not bound.
  509.  
  510. File: readline.info,  Node: Commands For Completion,  Next: Miscellaneous Commands,  Prev: Numeric Arguments,  Up: Readline Init Syntax
  511.  
  512. Letting Readline Type For You
  513. .............................
  514.  
  515. `complete (TAB)'
  516.      Attempt to do completion on the text before point.  This is
  517.      implementation defined.  Generally, if you are typing a filename
  518.      argument, you can do filename completion; if you are typing a
  519.      command, you can do command completion, if you are typing in a
  520.      symbol to GDB, you can do symbol name completion, if you are
  521.      typing in a variable to Bash, you can do variable name
  522.      completion...
  523.  
  524. `possible-completions (M-?)'
  525.      List the possible completions of the text before point.
  526.  
  527. File: readline.info,  Node: Miscellaneous Commands,  Prev: Commands For Completion,  Up: Readline Init Syntax
  528.  
  529. Some Miscellaneous Commands
  530. ...........................
  531.  
  532. `re-read-init-file (C-x C-r)'
  533.      Read in the contents of your `~/.inputrc' file, and incorporate
  534.      any bindings found there.
  535.  
  536. `abort (C-g)'
  537.      Ding!  Stops things.
  538.  
  539. `do-uppercase-version (M-a, M-b, ...)'
  540.      Run the command that is bound to your uppercase brother.
  541.  
  542. `prefix-meta (ESC)'
  543.      Make the next character that you type be metafied.  This is for
  544.      people without a meta key.  Typing `ESC f' is equivalent to typing
  545.      `M-f'.
  546.  
  547. `undo (C-_)'
  548.      Incremental undo, separately remembered for each line.
  549.  
  550. `revert-line (M-r)'
  551.      Undo all changes made to this line.  This is like typing the
  552.      `undo' command enough times to get back to the beginning.
  553.  
  554. File: readline.info,  Node: Readline Vi Mode,  Prev: Readline Init Syntax,  Up: Readline Init File
  555.  
  556. Readline Vi Mode
  557. ----------------
  558.  
  559.    While the Readline library does not have a full set of Vi editing
  560. functions, it does contain enough to allow simple editing of the line.
  561.  
  562.    In order to switch interactively between Emacs and Vi editing
  563. modes, use the command M-C-j (toggle-editing-mode).
  564.  
  565.    When you enter a line in Vi mode, you are already placed in
  566. `insertion' mode, as if you had typed an `i'.  Pressing ESC switches
  567. you into `edit' mode, where you can edit the text of the line with the
  568. standard Vi movement keys, move to previous history lines with `k',
  569. and following lines with `j', and so forth.
  570.  
  571.    This document describes the GNU Readline Library, a utility for
  572. aiding in the consitency of user interface across discrete programs
  573. that need to provide a command line interface.
  574.  
  575.    Copyright (C) 1988 Free Software Foundation, Inc.
  576.  
  577.    Permission is granted to make and distribute verbatim copies of
  578. this manual provided the copyright notice and this permission notice
  579. pare preserved on all copies.
  580.  
  581.    Permission is granted to copy and distribute modified versions of
  582. this manual under the conditions for verbatim copying, provided that
  583. the entire resulting derived work is distributed under the terms of a
  584. permission notice identical to this one.
  585.  
  586.    Permission is granted to copy and distribute translations of this
  587. manual into another language, under the above conditions for modified
  588. versions, except that this permission notice may be stated in a
  589. translation approved by the Foundation.
  590.  
  591. File: readline.info,  Node: Programming with GNU Readline,  Next: Concept Index,  Prev: Command Line Editing,  Up: Top
  592.  
  593. Programming with GNU Readline
  594. *****************************
  595.  
  596.    This manual describes the interface between the GNU Readline
  597. Library and user programs.  If you are a programmer, and you wish to
  598. include the features found in GNU Readline in your own programs, such
  599. as completion, line editing, and interactive history manipulation,
  600. this documentation is for you.
  601.  
  602. * Menu:
  603.  
  604. * Default Behaviour::    Using the default behaviour of Readline.
  605. * Custom Functions::    Adding your own functions to Readline.
  606. * Custom Completers::    Supplanting or supplementing Readline's
  607.             completion functions.
  608.  
  609. File: readline.info,  Node: Default Behaviour,  Next: Custom Functions,  Up: Programming with GNU Readline
  610.  
  611. Default Behaviour
  612. =================
  613.  
  614.    Many programs provide a command line interface, such as `mail',
  615. `ftp', and `sh'.  For such programs, the default behaviour of Readline
  616. is sufficient.  This section describes how to use Readline in the
  617. simplest way possible, perhaps to replace calls in your code to `gets
  618. ()'.
  619.  
  620.    The function `readline' prints a prompt and then reads and returns
  621. a single line of text from the user.  The line which `readline ()'
  622. returns is allocated with `malloc ()'; you should `free ()' the line
  623. when you are done with it.  The declaration for `readline' in ANSI C is
  624.  
  625.      `char *readline (char *PROMPT);'
  626.  
  627.    So, one might say
  628.  
  629.      `char *line = readline ("Enter a line: ");'
  630.  
  631.    in order to read a line of text from the user.
  632.  
  633.    The line which is returned has the final newline removed, so only
  634. the text of the line remains.
  635.  
  636.    If readline encounters an `EOF' while reading the line, and the
  637. line is empty at that point, then `(char *)NULL' is returned. 
  638. Otherwise, the line is ended just as if a newline was typed.
  639.  
  640.    If you want the user to be able to get at the line later, (with C-p
  641. for example), you must call `add_history ()' to save the line away in
  642. a "history" list of such lines.
  643.  
  644.      `add_history (line)';
  645.  
  646.    For full details on the GNU History Library, see the associated
  647. manual.
  648.  
  649.    It is polite to avoid saving empty lines on the history list, since
  650. it is rare than someone has a burning need to reuse a blank line. 
  651. Here is a function which usefully replaces the standard `gets ()'
  652. library function:
  653.  
  654.      /* A static variable for holding the line. */
  655.      static char *line_read = (char *)NULL;
  656.      
  657.      /* Read a string, and return a pointer to it.  Returns NULL on EOF. */
  658.      char *
  659.      do_gets ()
  660.      {
  661.        /* If the buffer has already been allocated, return the memory
  662.           to the free pool. */
  663.        if (line_read != (char *)NULL)
  664.          {
  665.            free (line_read);
  666.            line_read = (char *)NULL;
  667.          }
  668.      
  669.        /* Get a line from the user. */
  670.        line_read = readline ("");
  671.      
  672.        /* If the line has any text in it, save it on the history. */
  673.        if (line_read && *line_read)
  674.          add_history (line_read);
  675.      
  676.        return (line_read);
  677.      }
  678.  
  679.    The above code gives the user the default behaviour of TAB
  680. completion: completion on file names.  If you do not want readline to
  681. complete on filenames, you can change the binding of the TAB key with
  682. `rl_bind_key ()'.
  683.  
  684.      `int rl_bind_key (int KEY, (int (*)())FUNCTION);'
  685.  
  686.    `rl_bind_key ()' takes 2 arguments; KEY is the character that you
  687. want to bind, and FUNCTION is the address of the function to run when
  688. KEY is pressed.  Binding TAB to `rl_insert ()' makes TAB just insert
  689. itself.
  690.  
  691.    `rl_bind_key ()' returns non-zero if KEY is not a valid ASCII
  692. character code (between 0 and 255).
  693.  
  694.      `rl_bind_key ('\t', rl_insert);'
  695.  
  696.    This code should be executed once at the start of your program; you
  697. might write a function called `initialize_readline ()' which performs
  698. this and other desired initializations, such as installing custom
  699. completers, etc.
  700.  
  701. File: readline.info,  Node: Custom Functions,  Next: Custom Completers,  Prev: Default Behaviour,  Up: Programming with GNU Readline
  702.  
  703. Custom Functions
  704. ================
  705.  
  706.    Readline provides a great many functions for manipulating the text
  707. of the line.  But it isn't possible to anticipate the needs of all
  708. programs.  This section describes the various functions and variables
  709. defined in within the Readline library which allow a user program to
  710. add customized functionality to Readline.
  711.  
  712. * Menu:
  713.  
  714. * The Function Type::    C declarations to make code readable.
  715. * Function Naming::    How to give a function you write a name.
  716. * Keymaps::        Making keymaps.
  717. * Binding Keys::    Changing Keymaps.
  718. * Function Writing::    Variables and calling conventions.
  719. * Allowing Undoing::    How to make your functions undoable.
  720.  
  721. File: readline.info,  Node: The Function Type,  Next: Function Naming,  Up: Custom Functions
  722.  
  723. The Function Type
  724. -----------------
  725.  
  726.    For the sake of readabilty, we declare a new type of object, called
  727. "Function".  A `Function' is a C language function which returns an
  728. `int'.  The type declaration for `Function' is:
  729.  
  730. `typedef int Function ();'
  731.  
  732.    The reason for declaring this new type is to make it easier to write
  733. code describing pointers to C functions.  Let us say we had a variable
  734. called FUNC which was a pointer to a function.  Instead of the classic
  735. C declaration
  736.  
  737.    `int (*)()func;'
  738.  
  739.    we have
  740.  
  741.    `Function *func;'
  742.  
  743. File: readline.info,  Node: Function Naming,  Next: Keymaps,  Prev: The Function Type,  Up: Custom Functions
  744.  
  745. Naming a Function
  746. -----------------
  747.  
  748.    The user can dynamically change the bindings of keys while using
  749. Readline.  This is done by representing the function with a descriptive
  750. name.  The user is able to type the descriptive name when referring to
  751. the function.  Thus, in an init file, one might find
  752.  
  753.      Meta-Rubout:    backward-kill-word
  754.  
  755.    This binds the keystroke Meta-Rubout to the function
  756. *descriptively* named `backward-kill-word'.  You, as the programmer,
  757. should bind the functions you write to descriptive names as well. 
  758. Readline provides a function for doing that:
  759.  
  760.  * Function: rl_add_defun (CHAR *NAME, FUNCTION *FUNCTION, INT KEY)
  761.      Add NAME to the list of named functions.  Make FUNCTION be the
  762.      function that gets called.  If KEY is not -1, then bind it to
  763.      FUNCTION using `rl_bind_key ()'.
  764.  
  765.    Using this function alone is sufficient for most applications.  It
  766. is the recommended way to add a few functions to the default functions
  767. that Readline has built in already.  If you need to do more or
  768. different things than adding a function to Readline, you may need to
  769. use the underlying functions described below.
  770.  
  771. File: readline.info,  Node: Keymaps,  Next: Binding Keys,  Prev: Function Naming,  Up: Custom Functions
  772.  
  773. Selecting a Keymap
  774. ------------------
  775.  
  776.    Key bindings take place on a "keymap".  The keymap is the
  777. association between the keys that the user types and the functions that
  778. get run.  You can make your own keymaps, copy existing keymaps, and
  779. tell Readline which keymap to use.
  780.  
  781.  * Function: Keymap rl_make_bare_keymap ()
  782.      Returns a new, empty keymap.  The space for the keymap is
  783.      allocated with `malloc ()'; you should `free ()' it when you are
  784.      done.
  785.  
  786.  * Function: Keymap rl_copy_keymap (KEYMAP MAP)
  787.      Return a new keymap which is a copy of MAP.
  788.  
  789.  * Function: Keymap rl_make_keymap ()
  790.      Return a new keymap with the printing characters bound to
  791.      rl_insert, the lowercase Meta characters bound to run their
  792.      equivalents, and the Meta digits bound to produce numeric
  793.      arguments.
  794.  
  795. File: readline.info,  Node: Binding Keys,  Next: Function Writing,  Prev: Keymaps,  Up: Custom Functions
  796.  
  797. Binding Keys
  798. ------------
  799.  
  800.    You associate keys with functions through the keymap.  Here are
  801. functions for doing that.
  802.  
  803.  * Function: int rl_bind_key (INT KEY, FUNCTION *FUNCTION)
  804.      Binds KEY to FUNCTION in the currently selected keymap.  Returns
  805.      non-zero in the case of an invalid KEY.
  806.  
  807.  * Function: int rl_bind_key_in_map (INT KEY, FUNCTION *FUNCTION,
  808.           KEYMAP MAP)
  809.      Bind KEY to FUNCTION in MAP.  Returns non-zero in the case of an
  810.      invalid KEY.
  811.  
  812.  * Function: int rl_unbind_key (INT KEY)
  813.      Make KEY do nothing in the currently selected keymap.  Returns
  814.      non-zero in case of error.
  815.  
  816.  * Function: int rl_unbind_key_in_map (INT KEY, KEYMAP MAP)
  817.      Make KEY be bound to the null function in MAP.  Returns non-zero
  818.      in case of error.
  819.  
  820.  * Function: rl_generic_bind (INT TYPE, CHAR *KEYSEQ, CHAR *DATA,
  821.           KEYMAP MAP)
  822.      Bind the key sequence represented by the string KEYSEQ to the
  823.      arbitrary pointer DATA.  TYPE says what kind of data is pointed
  824.      to by DATA; right now this can be a function (`ISFUNC'), a macro
  825.      (`ISMACR'), or a keymap (`ISKMAP').  This makes new keymaps as
  826.      necessary.  The initial place to do bindings is in MAP.
  827.  
  828. File: readline.info,  Node: Function Writing,  Next: Allowing Undoing,  Prev: Binding Keys,  Up: Custom Functions
  829.  
  830. Writing a New Function
  831. ----------------------
  832.  
  833.    In order to write new functions for Readline, you need to know the
  834. calling conventions for keyboard invoked functions, and the names of
  835. the variables that describe the current state of the line gathered so
  836. far.
  837.  
  838.  * Variable: char *rl_line_buffer
  839.      This is the line gathered so far.  You are welcome to modify the
  840.      contents of this, but see Undoing, below.
  841.  
  842.  * Variable: int rl_point
  843.      The offset of the current cursor position in RL_LINE_BUFFER.
  844.  
  845.  * Variable: int rl_end
  846.      The number of characters present in `rl_line_buffer'.  When
  847.      `rl_point' is at the end of the line, then `rl_point' and
  848.      `rl_end' are equal.
  849.  
  850.    The calling sequence for a command `foo' looks like
  851.  
  852.      `foo (int count, int key)'
  853.  
  854.    where COUNT is the numeric argument (or 1 if defaulted) and KEY is
  855. the key that invoked this function.
  856.  
  857.    It is completely up to the function as to what should be done with
  858. the numeric argument; some functions use it as a repeat count, other
  859. functions as a flag, and some choose to ignore it.  In general, if a
  860. function uses the numeric argument as a repeat count, it should be able
  861. to do something useful with a negative argument as well as a positive
  862. argument.  At the very least, it should be aware that it can be passed
  863. a negative argument.
  864.  
  865. File: readline.info,  Node: Allowing Undoing,  Prev: Function Writing,  Up: Custom Functions
  866.  
  867. Allowing Undoing
  868. ----------------
  869.  
  870.    Supporting the undo command is a painless thing to do, and makes
  871. your functions much more useful to the end user.  It is certainly easy
  872. to try something if you know you can undo it.  I could use an undo
  873. function for the stock market.
  874.  
  875.    If your function simply inserts text once, or deletes text once,
  876. and it calls `rl_insert_text ()' or `rl_delete_text ()' to do it, then
  877. undoing is already done for you automatically, and you can safely skip
  878. this section.
  879.  
  880.    If you do multiple insertions or multiple deletions, or any
  881. combination of these operations, you should group them together into
  882. one operation.  This can be done with `rl_begin_undo_group ()' and
  883. `rl_end_undo_group ()'.
  884.  
  885.  * Function: rl_begin_undo_group ()
  886.      Begins saving undo information in a group construct.  The undo
  887.      information usually comes from calls to `rl_insert_text ()' and
  888.      `rl_delete_text ()', but they could be direct calls to
  889.      `rl_add_undo ()'.
  890.  
  891.  * Function: rl_end_undo_group ()
  892.      Closes the current undo group started with `rl_begin_undo_group
  893.      ()'.  There should be exactly one call to `rl_end_undo_group ()'
  894.      for every call to `rl_begin_undo_group ()'.
  895.  
  896.    Finally, if you neither insert nor delete text, but directly modify
  897. the existing text (e.g. change its case), you call `rl_modifying ()'
  898. once, just before you modify the text.  You must supply the indices of
  899. the text range that you are going to modify.
  900.  
  901.  * Function: rl_modifying (INT START, INT END)
  902.      Tell Readline to save the text between START and END as a single
  903.      undo unit.  It is assumed that subsequent to this call you will
  904.      modify that range of text in some way.
  905.  
  906. An Example
  907. ----------
  908.  
  909.    Here is a function which changes lowercase characters to the
  910. uppercase equivalents, and uppercase characters to the lowercase
  911. equivalents.  If this function was bound to `M-c', then typing `M-c'
  912. would change the case of the character under point.  Typing `10 M-c'
  913. would change the case of the following 10 characters, leaving the
  914. cursor on the last character changed.
  915.  
  916.      /* Invert the case of the COUNT following characters. */
  917.      invert_case_line (count, key)
  918.           int count, key;
  919.      {
  920.        register int start, end;
  921.      
  922.        start = rl_point;
  923.      
  924.        if (count < 0)
  925.          {
  926.            direction = -1;
  927.            count = -count;
  928.          }
  929.        else
  930.          direction = 1;
  931.      
  932.        /* Find the end of the range to modify. */
  933.        end = start + (count * direction);
  934.      
  935.        /* Force it to be within range. */
  936.        if (end > rl_end)
  937.          end = rl_end;
  938.        else if (end < 0)
  939.          end = -1;
  940.      
  941.        if (start > end)
  942.          {
  943.            int temp = start;
  944.            start = end;
  945.            end = temp;
  946.          }
  947.      
  948.        if (start == end)
  949.          return;
  950.      
  951.        /* Tell readline that we are modifying the line, so save the undo
  952.           information. */
  953.        rl_modifying (start, end);
  954.      
  955.        for (; start != end; start += direction)
  956.          {
  957.            if (uppercase_p (rl_line_buffer[start]))
  958.              rl_line_buffer[start] = to_lower (rl_line_buffer[start]);
  959.            else if (lowercase_p (rl_line_buffer[start]))
  960.              rl_line_buffer[start] = to_upper (rl_line_buffer[start]);
  961.          }
  962.        /* Move point to on top of the last character changed. */
  963.        rl_point = end - direction;
  964.      }
  965.  
  966. File: readline.info,  Node: Custom Completers,  Prev: Custom Functions,  Up: Programming with GNU Readline
  967.  
  968. Custom Completers
  969. =================
  970.  
  971.    Typically, a program that reads commands from the user has a way of
  972. disambiguating commands and data.  If your program is one of these,
  973. then it can provide completion for either commands, or data, or both
  974. commands and data.  The following sections describe how your program
  975. and Readline cooperate to provide this service to end users.
  976.  
  977. * Menu:
  978.  
  979. * How Completing Works::    The logic used to do completion.
  980. * Completion Functions::    Functions provided by Readline.
  981. * Completion Variables::    Variables which control completion.
  982. * A Short Completion Example::    An example of writing completer subroutines.
  983.  
  984. File: readline.info,  Node: How Completing Works,  Next: Completion Functions,  Up: Custom Completers
  985.  
  986. How Completing Works
  987. --------------------
  988.  
  989.    In order to complete some text, the full list of possible
  990. completions must be available.  That is to say, it is not possible to
  991. accurately expand a partial word without knowing what all of the
  992. possible words that make sense in that context are.  The GNU Readline
  993. library provides the user interface to completion, and additionally,
  994. two of the most common completion functions; filename and username. 
  995. For completing other types of text, you must write your own completion
  996. function.  This section describes exactly what those functions must
  997. do, and provides an example function.
  998.  
  999.    There are three major functions used to perform completion:
  1000.  
  1001.   1. The user-interface function `rl_complete ()'.  This function is
  1002.      called interactively with the same calling conventions as other
  1003.      functions in readline intended for interactive use; i.e. COUNT,
  1004.      and INVOKING-KEY.  It isolates the word to be completed and calls
  1005.      `completion_matches ()' to generate a list of possible
  1006.      completions.  It then either lists the possible completions or
  1007.      actually performs the completion, depending on which behaviour is
  1008.      desired.
  1009.  
  1010.   2. The internal function `completion_matches ()' uses your
  1011.      "generator" function to generate the list of possible matches, and
  1012.      then returns the array of these matches.  You should place the
  1013.      address of your generator function in
  1014.      `rl_completion_entry_function'.
  1015.  
  1016.   3. The generator function is called repeatedly from
  1017.      `completion_matches ()', returning a string each time.  The
  1018.      arguments to the generator function are TEXT and STATE.  TEXT is
  1019.      the partial word to be completed.  STATE is zero the first time
  1020.      the function is called, and a positive non-zero integer for each
  1021.      subsequent call.  When the generator function returns `(char
  1022.      *)NULL' this signals `completion_matches ()' that there are no
  1023.      more possibilities left.
  1024.  
  1025.  * Function: rl_complete (INT IGNORE, INT INVOKING_KEY)
  1026.      Complete the word at or before point.  You have supplied the
  1027.      function that does the initial simple matching selection
  1028.      algorithm (see `completion_matches ()').  The default is to do
  1029.      filename completion.
  1030.  
  1031.    Note that `rl_complete ()' has the identical calling conventions as
  1032. any other key-invokable function; this is because by default it is
  1033. bound to the `TAB' key.
  1034.  
  1035.  * Variable: Function *rl_completion_entry_function
  1036.      This is a pointer to the generator function for
  1037.      `completion_matches ()'.  If the value of
  1038.      `rl_completion_entry_function' is `(Function *)NULL' then the
  1039.      default filename generator function is used, namely
  1040.      `filename_entry_function ()'.
  1041.  
  1042. File: readline.info,  Node: Completion Functions,  Next: Completion Variables,  Prev: How Completing Works,  Up: Custom Completers
  1043.  
  1044. Completion Functions
  1045. --------------------
  1046.  
  1047.    Here is the complete list of callable completion functions present
  1048. in Readline.
  1049.  
  1050.  * Function: rl_complete_internal (INT WHAT_TO_DO)
  1051.      Complete the word at or before point.  WHAT_TO_DO says what to do
  1052.      with the completion.  A value of `?' means list the possible
  1053.      completions.  `TAB' means do standard completion.  `*' means
  1054.      insert all of the possible completions.
  1055.  
  1056.  * Function: rl_complete (INT IGNORE, INT INVOKING_KEY)
  1057.      Complete the word at or before point.  You have supplied the
  1058.      function that does the initial simple matching selection
  1059.      algorithm (see `completion_matches ()').  The default is to do
  1060.      filename completion.  This just calls `rl_complete_internal ()'
  1061.      with an argument of `TAB'.
  1062.  
  1063.  * Function: rl_possible_completions ()
  1064.      List the possible completions.  See description of `rl_complete
  1065.      ()'.  This just calls `rl_complete_internal ()' with an argument
  1066.      of `?'.
  1067.  
  1068.  * Function: char **completion_matches (CHAR *TEXT, CHAR
  1069.           *(*ENTRY_FUNCTION) ())
  1070.      Returns an array of `(char *)' which is a list of completions for
  1071.      TEXT.  If there are no completions, returns `(char **)NULL'.  The
  1072.      first entry in the returned array is the substitution for TEXT. 
  1073.      The remaining entries are the possible completions.  The array is
  1074.      terminated with a `NULL' pointer.
  1075.  
  1076.      ENTRY_FUNCTION is a function of two args, and returns a `(char
  1077.      *)'.  The first argument is TEXT.  The second is a state
  1078.      argument; it is zero on the first call, and non-zero on subsequent
  1079.      calls.  It returns a `NULL'  pointer to the caller when there are
  1080.      no more matches.
  1081.  
  1082.  * Function: char *filename_completion_function (CHAR *TEXT, INT STATE)
  1083.      A generator function for filename completion in the general case.
  1084.       Note that completion in the Bash shell is a little different
  1085.      because of all the pathnames that must be followed when looking
  1086.      up the completion for a command.
  1087.  
  1088.  * Function: char *username_completion_function (CHAR *TEXT, INT STATE)
  1089.      A completion generator for usernames.  TEXT contains a partial
  1090.      username preceded by a random character (usually `~').
  1091.  
  1092. File: readline.info,  Node: Completion Variables,  Next: A Short Completion Example,  Prev: Completion Functions,  Up: Custom Completers
  1093.  
  1094. Completion Variables
  1095. --------------------
  1096.  
  1097.  * Variable: Function *rl_completion_entry_function
  1098.      A pointer to the generator function for `completion_matches ()'. 
  1099.      `NULL' means to use `filename_entry_function ()', the default
  1100.      filename completer.
  1101.  
  1102.  * Variable: Function *rl_attempted_completion_function
  1103.      A pointer to an alternative function to create matches.  The
  1104.      function is called with TEXT, START, and END.  START and END are
  1105.      indices in `rl_line_buffer' saying what the boundaries of TEXT
  1106.      are.  If this function exists and returns `NULL' then
  1107.      `rl_complete ()' will call the value of
  1108.      `rl_completion_entry_function' to generate matches, otherwise the
  1109.      array of strings returned will be used.
  1110.  
  1111.  * Variable: int rl_completion_query_items
  1112.      Up to this many items will be displayed in response to a
  1113.      possible-completions call.  After that, we ask the user if she is
  1114.      sure she wants to see them all.  The default value is 100.
  1115.  
  1116.  * Variable: char *rl_basic_word_break_characters
  1117.      The basic list of characters that signal a break between words
  1118.      for the completer routine.  The contents of this variable is what
  1119.      breaks words in the Bash shell, i.e. " \t\n\"\\'`@$><=;|&{(".
  1120.  
  1121.  * Variable: char *rl_completer_word_break_characters
  1122.      The list of characters that signal a break between words for
  1123.      `rl_complete_internal ()'.  The default list is the contents of
  1124.      `rl_basic_word_break_characters'.
  1125.  
  1126.  * Variable: char *rl_special_prefixes
  1127.      The list of characters that are word break characters, but should
  1128.      be left in TEXT when it is passed to the completion function. 
  1129.      Programs can use this to help determine what kind of completing
  1130.      to do.
  1131.  
  1132.  * Variable: int rl_ignore_completion_duplicates
  1133.      If non-zero, then disallow duplicates in the matches.  Default is
  1134.      1.
  1135.  
  1136.  * Variable: int rl_filename_completion_desired
  1137.      Non-zero means that the results of the matches are to be treated
  1138.      as filenames.  This is *always* zero on entry, and can only be
  1139.      changed within a completion entry generator function.
  1140.  
  1141.  * Variable: Function *rl_ignore_some_completions_function
  1142.      This function, if defined, is called by the completer when real
  1143.      filename completion is done, after all the matching names have
  1144.      been generated.  It is passed a `NULL' terminated array of `(char
  1145.      *)' known as MATCHES in the code.  The 1st element (`matches[0]')
  1146.      is the maximal substring that is common to all matches. This
  1147.      function can re-arrange the list of matches as required, but each
  1148.      deleted element of the array must be `free()''d.
  1149.  
  1150. File: readline.info,  Node: A Short Completion Example,  Prev: Completion Variables,  Up: Custom Completers
  1151.  
  1152. A Short Completion Example
  1153. --------------------------
  1154.  
  1155.    Here is a small application demonstrating the use of the GNU
  1156. Readline library.  It is called `fileman', and the source code resides
  1157. in `readline/examples/fileman.c'.  This sample application provides
  1158. completion of command names, line editing features, and access to the
  1159. history list.
  1160.  
  1161.      /* fileman.c -- A tiny application which demonstrates how to use the
  1162.         GNU Readline library.  This application interactively allows users
  1163.         to manipulate files and their modes. */
  1164.      
  1165.      #include <stdio.h>
  1166.      #include <readline/readline.h>
  1167.      #include <readline/history.h>
  1168.      #include <sys/types.h>
  1169.      #include <sys/file.h>
  1170.      #include <sys/stat.h>
  1171.      #include <sys/errno.h>
  1172.      
  1173.      /* The names of functions that actually do the manipulation. */
  1174.      int com_list (), com_view (), com_rename (), com_stat (), com_pwd ();
  1175.      int com_delete (), com_help (), com_cd (), com_quit ();
  1176.      
  1177.      /* A structure which contains information on the commands this program
  1178.         can understand. */
  1179.      
  1180.      typedef struct {
  1181.        char *name;                   /* User printable name of the function. */
  1182.        Function *func;               /* Function to call to do the job. */
  1183.        char *doc;                    /* Documentation for this function.  */
  1184.      } COMMAND;
  1185.      
  1186.      COMMAND commands[] = {
  1187.        { "cd", com_cd, "Change to directory DIR" },
  1188.        { "delete", com_delete, "Delete FILE" },
  1189.        { "help", com_help, "Display this text" },
  1190.        { "?", com_help, "Synonym for `help'" },
  1191.        { "list", com_list, "List files in DIR" },
  1192.        { "ls", com_list, "Synonym for `list'" },
  1193.        { "pwd", com_pwd, "Print the current working directory" },
  1194.        { "quit", com_quit, "Quit using Fileman" },
  1195.        { "rename", com_rename, "Rename FILE to NEWNAME" },
  1196.        { "stat", com_stat, "Print out statistics on FILE" },
  1197.        { "view", com_view, "View the contents of FILE" },
  1198.        { (char *)NULL, (Function *)NULL, (char *)NULL }
  1199.      };
  1200.      
  1201.      /* The name of this program, as taken from argv[0]. */
  1202.      char *progname;
  1203.      
  1204.      /* When non-zero, this global means the user is done using this program. */
  1205.      int done = 0;
  1206.      
  1207.      main (argc, argv)
  1208.           int argc;
  1209.           char **argv;
  1210.      {
  1211.        progname = argv[0];
  1212.      
  1213.        initialize_readline ();       /* Bind our completer. */
  1214.      
  1215.        /* Loop reading and executing lines until the user quits. */
  1216.        while (!done)
  1217.          {
  1218.            char *line;
  1219.      
  1220.            line = readline ("FileMan: ");
  1221.      
  1222.            if (!line)
  1223.              {
  1224.                done = 1;             /* Encountered EOF at top level. */
  1225.              }
  1226.            else
  1227.              {
  1228.                /* Remove leading and trailing whitespace from the line.
  1229.                   Then, if there is anything left, add it to the history list
  1230.                   and execute it. */
  1231.                stripwhite (line);
  1232.      
  1233.                if (*line)
  1234.                  {
  1235.                    add_history (line);
  1236.                    execute_line (line);
  1237.                  }
  1238.              }
  1239.      
  1240.            if (line)
  1241.              free (line);
  1242.          }
  1243.        exit (0);
  1244.      }
  1245.      
  1246.      /* Execute a command line. */
  1247.      execute_line (line)
  1248.           char *line;
  1249.      {
  1250.        register int i;
  1251.        COMMAND *find_command (), *command;
  1252.        char *word;
  1253.      
  1254.        /* Isolate the command word. */
  1255.        i = 0;
  1256.        while (line[i] && !whitespace (line[i]))
  1257.          i++;
  1258.      
  1259.        word = line;
  1260.      
  1261.        if (line[i])
  1262.          line[i++] = '\0';
  1263.      
  1264.        command = find_command (word);
  1265.      
  1266.        if (!command)
  1267.          {
  1268.            fprintf (stderr, "%s: No such command for FileMan.\n", word);
  1269.            return;
  1270.          }
  1271.      
  1272.        /* Get argument to command, if any. */
  1273.        while (whitespace (line[i]))
  1274.          i++;
  1275.      
  1276.        word = line + i;
  1277.      
  1278.        /* Call the function. */
  1279.        (*(command->func)) (word);
  1280.      }
  1281.      
  1282.      /* Look up NAME as the name of a command, and return a pointer to that
  1283.         command.  Return a NULL pointer if NAME isn't a command name. */
  1284.      COMMAND *
  1285.      find_command (name)
  1286.           char *name;
  1287.      {
  1288.        register int i;
  1289.      
  1290.        for (i = 0; commands[i].name; i++)
  1291.          if (strcmp (name, commands[i].name) == 0)
  1292.            return (&commands[i]);
  1293.      
  1294.        return ((COMMAND *)NULL);
  1295.      }
  1296.      
  1297.      /* Strip whitespace from the start and end of STRING. */
  1298.      stripwhite (string)
  1299.           char *string;
  1300.      {
  1301.        register int i = 0;
  1302.      
  1303.        while (whitespace (string[i]))
  1304.          i++;
  1305.      
  1306.        if (i)
  1307.          strcpy (string, string + i);
  1308.      
  1309.        i = strlen (string) - 1;
  1310.      
  1311.        while (i > 0 && whitespace (string[i]))
  1312.          i--;
  1313.      
  1314.        string[++i] = '\0';
  1315.      }
  1316.      
  1317.      /* **************************************************************** */
  1318.      /*                                                                  */
  1319.      /*                  Interface to Readline Completion                */
  1320.      /*                                                                  */
  1321.      /* **************************************************************** */
  1322.      
  1323.      /* Tell the GNU Readline library how to complete.  We want to try to complete
  1324.         on command names if this is the first word in the line, or on filenames
  1325.         if not. */
  1326.      initialize_readline ()
  1327.      {
  1328.        char **fileman_completion ();
  1329.      
  1330.        /* Allow conditional parsing of the ~/.inputrc file. */
  1331.        rl_readline_name = "FileMan";
  1332.      
  1333.        /* Tell the completer that we want a crack first. */
  1334.        rl_attempted_completion_function = (Function *)fileman_completion;
  1335.      }
  1336.      
  1337.      /* Attempt to complete on the contents of TEXT.  START and END show the
  1338.         region of TEXT that contains the word to complete.  We can use the
  1339.         entire line in case we want to do some simple parsing.  Return the
  1340.         array of matches, or NULL if there aren't any. */
  1341.      char **
  1342.      fileman_completion (text, start, end)
  1343.           char *text;
  1344.           int start, end;
  1345.      {
  1346.        char **matches;
  1347.        char *command_generator ();
  1348.      
  1349.        matches = (char **)NULL;
  1350.      
  1351.        /* If this word is at the start of the line, then it is a command
  1352.           to complete.  Otherwise it is the name of a file in the current
  1353.           directory. */
  1354.        if (start == 0)
  1355.          matches = completion_matches (text, command_generator);
  1356.      
  1357.        return (matches);
  1358.      }
  1359.      
  1360.      /* Generator function for command completion.  STATE lets us know whether
  1361.         to start from scratch; without any state (i.e. STATE == 0), then we
  1362.         start at the top of the list. */
  1363.      char *
  1364.      command_generator (text, state)
  1365.           char *text;
  1366.           int state;
  1367.      {
  1368.        static int list_index, len;
  1369.        char *name;
  1370.      
  1371.        /* If this is a new word to complete, initialize now.  This includes
  1372.           saving the length of TEXT for efficiency, and initializing the index
  1373.           variable to 0. */
  1374.        if (!state)
  1375.          {
  1376.            list_index = 0;
  1377.            len = strlen (text);
  1378.          }
  1379.      
  1380.        /* Return the next name which partially matches from the command list. */
  1381.        while (name = commands[list_index].name)
  1382.          {
  1383.            list_index++;
  1384.      
  1385.            if (strncmp (name, text, len) == 0)
  1386.              return (name);
  1387.          }
  1388.      
  1389.        /* If no names matched, then return NULL. */
  1390.        return ((char *)NULL);
  1391.      }
  1392.      
  1393.      /* **************************************************************** */
  1394.      /*                                                                  */
  1395.      /*                       FileMan Commands                           */
  1396.      /*                                                                  */
  1397.      /* **************************************************************** */
  1398.      
  1399.      /* String to pass to system ().  This is for the LIST, VIEW and RENAME
  1400.         commands. */
  1401.      static char syscom[1024];
  1402.      
  1403.      /* List the file(s) named in arg. */
  1404.      com_list (arg)
  1405.           char *arg;
  1406.      {
  1407.        if (!arg)
  1408.          arg = "*";
  1409.      
  1410.        sprintf (syscom, "ls -FClg %s", arg);
  1411.        system (syscom);
  1412.      }
  1413.      
  1414.      com_view (arg)
  1415.           char *arg;
  1416.      {
  1417.        if (!valid_argument ("view", arg))
  1418.          return;
  1419.      
  1420.        sprintf (syscom, "cat %s | more", arg);
  1421.        system (syscom);
  1422.      }
  1423.      
  1424.      com_rename (arg)
  1425.           char *arg;
  1426.      {
  1427.        too_dangerous ("rename");
  1428.      }
  1429.      
  1430.      com_stat (arg)
  1431.           char *arg;
  1432.      {
  1433.        struct stat finfo;
  1434.      
  1435.        if (!valid_argument ("stat", arg))
  1436.          return;
  1437.      
  1438.        if (stat (arg, &finfo) == -1)
  1439.          {
  1440.            perror (arg);
  1441.            return;
  1442.          }
  1443.      
  1444.        printf ("Statistics for `%s':\n", arg);
  1445.      
  1446.        printf ("%s has %d link%s, and is %d bytes in length.\n", arg,
  1447.                finfo.st_nlink, (finfo.st_nlink == 1) ? "" : "s",  finfo.st_size);
  1448.        printf ("      Created on: %s", ctime (&finfo.st_ctime));
  1449.        printf ("  Last access at: %s", ctime (&finfo.st_atime));
  1450.        printf ("Last modified at: %s", ctime (&finfo.st_mtime));
  1451.      }
  1452.      
  1453.      com_delete (arg)
  1454.           char *arg;
  1455.      {
  1456.        too_dangerous ("delete");
  1457.      }
  1458.      
  1459.      /* Print out help for ARG, or for all of the commands if ARG is
  1460.         not present. */
  1461.      com_help (arg)
  1462.           char *arg;
  1463.      {
  1464.        register int i;
  1465.        int printed = 0;
  1466.      
  1467.        for (i = 0; commands[i].name; i++)
  1468.          {
  1469.            if (!*arg || (strcmp (arg, commands[i].name) == 0))
  1470.              {
  1471.                printf ("%s\t\t%s.\n", commands[i].name, commands[i].doc);
  1472.                printed++;
  1473.              }
  1474.          }
  1475.      
  1476.        if (!printed)
  1477.          {
  1478.            printf ("No commands match `%s'.  Possibilties are:\n", arg);
  1479.      
  1480.            for (i = 0; commands[i].name; i++)
  1481.              {
  1482.                /* Print in six columns. */
  1483.                if (printed == 6)
  1484.                  {
  1485.                    printed = 0;
  1486.                    printf ("\n");
  1487.                  }
  1488.      
  1489.                printf ("%s\t", commands[i].name);
  1490.                printed++;
  1491.              }
  1492.      
  1493.            if (printed)
  1494.              printf ("\n");
  1495.          }
  1496.      }
  1497.      
  1498.      /* Change to the directory ARG. */
  1499.      com_cd (arg)
  1500.           char *arg;
  1501.      {
  1502.        if (chdir (arg) == -1)
  1503.          perror (arg);
  1504.      
  1505.        com_pwd ("");
  1506.      }
  1507.      
  1508.      /* Print out the current working directory. */
  1509.      com_pwd (ignore)
  1510.           char *ignore;
  1511.      {
  1512.        char dir[1024];
  1513.      
  1514.        (void) getwd (dir);
  1515.      
  1516.        printf ("Current directory is %s\n", dir);
  1517.      }
  1518.      
  1519.      /* The user wishes to quit using this program.  Just set DONE non-zero. */
  1520.      com_quit (arg)
  1521.           char *arg;
  1522.      {
  1523.        done = 1;
  1524.      }
  1525.      
  1526.      /* Function which tells you that you can't do this. */
  1527.      too_dangerous (caller)
  1528.           char *caller;
  1529.      {
  1530.        fprintf (stderr,
  1531.                 "%s: Too dangerous for me to distribute.  Write it yourself.\n",
  1532.                 caller);
  1533.      }
  1534.      
  1535.      /* Return non-zero if ARG is a valid argument for CALLER, else print
  1536.         an error message and return zero. */
  1537.      int
  1538.      valid_argument (caller, arg)
  1539.           char *caller, *arg;
  1540.      {
  1541.        if (!arg || !*arg)
  1542.          {
  1543.            fprintf (stderr, "%s: Argument required.\n", caller);
  1544.            return (0);
  1545.          }
  1546.      
  1547.        return (1);
  1548.      }
  1549.  
  1550. File: readline.info,  Node: Concept Index,  Next: Function and Variable Index,  Prev: Programming with GNU Readline,  Up: Top
  1551.  
  1552. Concept Index
  1553. *************
  1554.  
  1555. * Menu:
  1556.  
  1557. * interaction, readline:                Readline Interaction.
  1558. * readline, function:                   Default Behaviour.
  1559.  
  1560. File: readline.info,  Node: Function and Variable Index,  Prev: Concept Index,  Up: Top
  1561.  
  1562. Function and Variable Index
  1563. ***************************
  1564.  
  1565. * Menu:
  1566.  
  1567. * Function *rl_attempted_completion_function: Completion Variables.
  1568. * Function *rl_completion_entry_function: Completion Variables.
  1569. * Function *rl_completion_entry_function: How Completing Works.
  1570. * Function *rl_ignore_some_completions_function: Completion Variables.
  1571. * Keymap rl_copy_keymap:                Keymaps.
  1572. * Keymap rl_make_bare_keymap:           Keymaps.
  1573. * Keymap rl_make_keymap:                Keymaps.
  1574. * abort (C-g):                          Miscellaneous Commands.
  1575. * accept-line (Newline, Return):        Commands For History.
  1576. * backward-char (C-b):                  Commands For Moving.
  1577. * backward-delete-char (Rubout):        Commands For Text.
  1578. * backward-kill-line ():                Commands For Killing.
  1579. * backward-kill-word (M-DEL):           Commands For Killing.
  1580. * backward-word (M-b):                  Commands For Moving.
  1581. * beginning-of-history (M-<):           Commands For History.
  1582. * beginning-of-line (C-a):              Commands For Moving.
  1583. * capitalize-word (M-c):                Commands For Text.
  1584. * char **completion_matches:            Completion Functions.
  1585. * char *filename_completion_function:   Completion Functions.
  1586. * char *rl_basic_word_break_characters: Completion Variables.
  1587. * char *rl_completer_word_break_characters: Completion Variables.
  1588. * char *rl_line_buffer:                 Function Writing.
  1589. * char *rl_special_prefixes:            Completion Variables.
  1590. * char *username_completion_function:   Completion Functions.
  1591. * clear-screen (C-l):                   Commands For Moving.
  1592. * complete (TAB):                       Commands For Completion.
  1593. * delete-char (C-d):                    Commands For Text.
  1594. * digit-argument (M-0, M-1, ... M--):   Numeric Arguments.
  1595. * do-uppercase-version (M-a, M-b, ...): Miscellaneous Commands.
  1596. * downcase-word (M-l):                  Commands For Text.
  1597. * editing-mode:                         Readline Init Syntax.
  1598. * end-of-history (M->):                 Commands For History.
  1599. * end-of-line (C-e):                    Commands For Moving.
  1600. * forward-char (C-f):                   Commands For Moving.
  1601. * forward-search-history (C-s):         Commands For History.
  1602. * forward-word (M-f):                   Commands For Moving.
  1603. * horizontal-scroll-mode:               Readline Init Syntax.
  1604. * int rl_bind_key:                      Binding Keys.
  1605. * int rl_bind_key_in_map:               Binding Keys.
  1606. * int rl_completion_query_items:        Completion Variables.
  1607. * int rl_end:                           Function Writing.
  1608. * int rl_filename_completion_desired:   Completion Variables.
  1609. * int rl_ignore_completion_duplicates:  Completion Variables.
  1610. * int rl_point:                         Function Writing.
  1611. * int rl_unbind_key:                    Binding Keys.
  1612. * int rl_unbind_key_in_map:             Binding Keys.
  1613. * kill-line (C-k):                      Commands For Killing.
  1614. * kill-word (M-d):                      Commands For Killing.
  1615. * mark-modified-lines:                  Readline Init Syntax.
  1616. * next-history (C-n):                   Commands For History.
  1617. * possible-completions (M-?):           Commands For Completion.
  1618. * prefer-visible-bell:                  Readline Init Syntax.
  1619. * prefix-meta (ESC):                    Miscellaneous Commands.
  1620. * previous-history (C-p):               Commands For History.
  1621. * quoted-insert (C-q, C-v):             Commands For Text.
  1622. * re-read-init-file (C-x C-r):          Miscellaneous Commands.
  1623. * readline ():                          Default Behaviour.
  1624. * reverse-search-history (C-r):         Commands For History.
  1625. * revert-line (M-r):                    Miscellaneous Commands.
  1626. * rl_add_defun:                         Function Naming.
  1627. * rl_begin_undo_group:                  Allowing Undoing.
  1628. * rl_bind_key ():                       Default Behaviour.
  1629. * rl_complete:                          How Completing Works.
  1630. * rl_complete:                          Completion Functions.
  1631. * rl_complete_internal:                 Completion Functions.
  1632. * rl_end_undo_group:                    Allowing Undoing.
  1633. * rl_generic_bind:                      Binding Keys.
  1634. * rl_modifying:                         Allowing Undoing.
  1635. * rl_possible_completions:              Completion Functions.
  1636. * self-insert (a, b, A, 1, !, ...):     Commands For Text.
  1637. * tab-insert (M-TAB):                   Commands For Text.
  1638. * transpose-chars (C-t):                Commands For Text.
  1639. * transpose-words (M-t):                Commands For Text.
  1640. * undo (C-_):                           Miscellaneous Commands.
  1641. * universal-argument ():                Numeric Arguments.
  1642. * unix-line-discard (C-u):              Commands For Killing.
  1643. * unix-word-rubout (C-w):               Commands For Killing.
  1644. * upcase-word (M-u):                    Commands For Text.
  1645. * yank (C-y):                           Commands For Killing.
  1646. * yank-pop (M-y):                       Commands For Killing.
  1647.  
  1648.  
  1649. Tag Table:
  1650. Node: Top998
  1651. Node: Command Line Editing1611
  1652. Node: Introduction and Notation2034
  1653. Node: Readline Interaction3056
  1654. Node: Readline Bare Essentials4195
  1655. Node: Readline Movement Commands5703
  1656. Node: Readline Killing Commands6594
  1657. Node: Readline Arguments8438
  1658. Node: Readline Init File9390
  1659. Node: Readline Init Syntax10218
  1660. Node: Commands For Moving14208
  1661. Node: Commands For History14838
  1662. Node: Commands For Text15913
  1663. Node: Commands For Killing17581
  1664. Node: Numeric Arguments18708
  1665. Node: Commands For Completion19152
  1666. Node: Miscellaneous Commands19876
  1667. Node: Readline Vi Mode20718
  1668. Node: Programming with GNU Readline22328
  1669. Node: Default Behaviour23033
  1670. Node: Custom Functions26258
  1671. Node: The Function Type27057
  1672. Node: Function Naming27690
  1673. Node: Keymaps28942
  1674. Node: Binding Keys29857
  1675. Node: Function Writing31158
  1676. Node: Allowing Undoing32599
  1677. Node: Custom Completers36101
  1678. Node: How Completing Works36849
  1679. Node: Completion Functions39664
  1680. Node: Completion Variables42000
  1681. Node: A Short Completion Example44772
  1682. Node: Concept Index56398
  1683. Node: Function and Variable Index56687
  1684. End Tag Table
  1685.